package com.chushouya.product.dao.repository;

import com.general.framework.core.enums.DelFlagEnum;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.ClothesProductEntity;
import com.chushouya.product.dao.mapper.ClothesProductEntityMapper;
import com.chushouya.product.dto.admin.clothesproduct.ClothesProductQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class ClothesProductRepository extends BaseRepository<ClothesProductEntityMapper, ClothesProductEntity, Long> {

    /**
     * 根据地区编码查询旧衣商品列表
     */
    public ClothesProductEntity selectByCode(Long provinceCode) {
        if (Objects.isNull(provinceCode)) {
            return null;
        }
        Example example = buildExample();
        example.and().andEqualTo("provinceCode", provinceCode);
        example.and().andEqualTo("delFlag", 0);
        return this.selectFirstOneByExample(example);
    }

    /**
     * 根据地区名称查询旧衣商品列表
     */
    public List<ClothesProductEntity> selectByDistrict(String district) {
        if (Strings.isBlank(district)) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andLike("district", Strings.format("%{}%", district));
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据商品名称模糊查询
     */
    public List<ClothesProductEntity> selectByProductNameLike(String productName) {
        if (Strings.isBlank(productName)) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andLike("productName", Strings.format("%{}%", productName));
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据状态查询旧衣商品列表
     */
    public List<ClothesProductEntity> selectByStatus(Integer status) {
        if (status == null) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andEqualTo("status", status);
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据操作人查询旧衣商品列表
     */
    public List<ClothesProductEntity> selectByHandleUser(String handleUser) {
        if (Strings.isBlank(handleUser)) {
            return Lists.emptyList();
        }
        Example example = buildExample();
        example.and().andLike("handleUser", Strings.format("%{}%", handleUser));
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 查询启用状态的旧衣商品
     */
    public List<ClothesProductEntity> selectActiveProducts() {
        Example example = buildExample();
        example.and().andEqualTo("status", 1);
        example.and().andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }

    /**
     * 根据查询条件查询单个记录
     */
    public ClothesProductEntity selectOne(ClothesProductQuery query) {
        return this.selectFirstOneByExample(buildExample(query));
    }

    /**
     * 根据查询条件查询列表
     */
    public List<ClothesProductEntity> selectList(ClothesProductQuery query) {
        return this.selectByExample(buildExample(query));
    }

    /**
     * 构建查询条件
     */
    private Example buildExample(ClothesProductQuery query) {
        Example example = this.buildExample();
        Example.Criteria criteria = example.createCriteria();
        
        if (Objects.nonNull(query.getClothesProductId())) {
            criteria.andEqualTo("clothesProductId", query.getClothesProductId());
        }
        if (Longs.notNullAndZero(query.getProvinceCode())) {
            criteria.andEqualTo("provinceCode", query.getProvinceCode());
        }
        if (Strings.isNotEmpty(query.getProvince())) {
            criteria.andLike("province", Strings.format("%{}%", query.getProvince()));
        }
        if (Strings.isNotEmpty(query.getProductName())) {
            criteria.andLike("productName", Strings.format("%{}%", query.getProductName()));
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        if (Objects.nonNull(query.getCreateTimeStart()) && Objects.nonNull(query.getCreateTimeEnd())) {
            criteria.andBetween("createTime", query.getCreateTimeStart(), query.getCreateTimeEnd());
        }
        criteria.andEqualTo("delFlag", DelFlagEnum.normal.getValue());
        
        return example;
    }
}
